﻿using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using EDB.SmoothStreaming.Models;
using EDB.SmoothStreaming.Settings;

namespace EDB.SmoothStreaming.Handlers {
    public class VideoHandler : ContentHandler {
        public VideoHandler(IRepository<VideoPartRecord> repository) {
            Filters.Add(StorageFilter.For(repository));
            OnInitializing<VideoPart>((context, part) => {
                part.Record.ThumbnailWidth = part.Settings.GetModel<VideoTypePartSettings>().ThumbnailWidthDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().ThumbnailWidthDefault;

                part.Record.ThumbnailHeight = part.Settings.GetModel<VideoTypePartSettings>().ThumbnailHeightDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().ThumbnailHeightDefault;

                part.Record.ThumbnailCount = part.Settings.GetModel<VideoTypePartSettings>().ThumbnailCountDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().ThumbnailCountDefault;

                part.Record.VideoStreams = part.Settings.GetModel<VideoTypePartSettings>().VideoStreamsDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().VideoStreamsDefault;

                part.Record.PlayerWidth = part.Settings.GetModel<VideoTypePartSettings>().PlayerWidthDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().PlayerWidthDefault;

                part.Record.PlayerHeight = part.Settings.GetModel<VideoTypePartSettings>().PlayerHeightDefault
                    ?? part.PartDefinition.Settings.GetModel<VideoPartSettings>().PlayerHeightDefault;

                part.Record.ThumbnailFormat = (int?)part.Settings.GetModel<VideoTypePartSettings>().ThumbnailFormatDefault
                    ?? (int)part.PartDefinition.Settings.GetModel<VideoPartSettings>().ThumbnailFormatDefault;
            });
        }
    }
}